package com.augurit.gzcgwsp.jzfqwczz.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.jzfqwczz.common.convert.YwYnJzfqwczzXckcConvertor;
import com.augurit.gzcgwsp.jzfqwczz.common.dao.YwYnJzfqwczzXckcDao;
import com.augurit.gzcgwsp.jzfqwczz.common.entity.YwYnJzfqwczzXckc;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnJzfqwczzXckcService;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnJzfqwczzXckcForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

@Service
@Transactional
public class YwYnJzfqwczzXckcServiceImpl implements IYwYnJzfqwczzXckcService {

	@Autowired
	private YwYnJzfqwczzXckcDao ywYnJzfqwczzXckcDao;
	
	@Transactional(readOnly = true)
	public YwYnJzfqwczzXckcForm get(Long id) {
		YwYnJzfqwczzXckc entity = ywYnJzfqwczzXckcDao.get(id);
		return YwYnJzfqwczzXckcConvertor.convertVoToForm(entity);
	}
	
	public void delete(Long... ids) {
		ywYnJzfqwczzXckcDao.delete(ids);
	}

	public void save(YwYnJzfqwczzXckcForm... forms) {
		if(forms != null)
			for(YwYnJzfqwczzXckcForm form : forms)
				this.save(form);
	}
	
	public void update(YwYnJzfqwczzXckcForm form) {
	    YwYnJzfqwczzXckcForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwYnJzfqwczzXckcForm form){
		
		if(form != null){
			YwYnJzfqwczzXckc entity = null;
			
			if(form != null && form.getId() != null){
				entity = ywYnJzfqwczzXckcDao.getYwYnJzfqwczzXckc(form.getId());
				if(entity.getId()==null){
					entity = new YwYnJzfqwczzXckc();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwYnJzfqwczzXckc();
			}
			
			YwYnJzfqwczzXckcConvertor.convertFormToVo(form, entity);
			if(org.apache.commons.lang.StringUtils.isNotEmpty(entity.getNeedKcsj()) && "2".endsWith(entity.getNeedKcsj())){
				entity.setKcsj(null);
			}
			ywYnJzfqwczzXckcDao.save(entity);
			
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwYnJzfqwczzXckcForm> search(Page<YwYnJzfqwczzXckcForm> page, YwYnJzfqwczzXckcForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwYnJzfqwczzXckc ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
		    if(StringUtils.isNotBlank(form.getTplcode())){
		    	hql.append(" and ps.templateCode = '" + form.getTplcode() + "'");
		    }
		}
		
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		ywYnJzfqwczzXckcDao.findPage(pg, hql.toString(), values);
		
		List<YwYnJzfqwczzXckcForm> list = YwYnJzfqwczzXckcConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Page<YwYnJzfqwczzXckcForm> search(Page<YwYnJzfqwczzXckcForm> page, List<PropertyFilter> filters) {
		Page<YwYnJzfqwczzXckc> pg = PageUtils.newPageInstance(page);
		
		ywYnJzfqwczzXckcDao.findPage(pg, filters);
		
		List<YwYnJzfqwczzXckcForm> list = YwYnJzfqwczzXckcConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwYnJzfqwczzXckc(YwYnJzfqwczzXckcForm form) {
	    YwYnJzfqwczzXckcForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
			YwYnJzfqwczzXckc entity = null;
			entity = ywYnJzfqwczzXckcDao.getYwYnJzfqwczzXckc(dbform.getId());
			YwYnJzfqwczzXckcConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				ywYnJzfqwczzXckcDao.updateYwYnJzfqwczzXckc(entity);
			}
	    }
	}

    public void saveEntity(YwYnJzfqwczzXckc entity) {
       this.ywYnJzfqwczzXckcDao.save(entity);
        
    }

	/**
	 * 保存现场勘察表返回id
	 */
	public Long saveEnterpriseInfo(YwYnJzfqwczzXckcForm form) {
		YwYnJzfqwczzXckc entity =new YwYnJzfqwczzXckc();
		
		YwYnJzfqwczzXckcConvertor.convertFormToVo(form, entity);
		
		return ywYnJzfqwczzXckcDao.saveEntity(entity);
	}
}
